<template>
  <div class="login">
    <div class="box">
      <p>牛牪犇后台管理系统</p>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="账号:" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码:" prop="password">
          <el-input v-model="ruleForm.password" type="password"></el-input>
        </el-form-item>
        <el-button type="primary" @click="login('ruleForm')">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>

export default {
  data(){
    return {
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入管理员账号', trigger: 'blur' },
          { min: 4, max: 6, message: '长度在 4 到 6 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入管理员密码', trigger: 'blur' },
          { min: 4, max: 6, message: '长度在 4 到 6 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    input(){
      // console.log(this.input);
    },
    pwd(){
      // console.log(this.pwd);
    }
  },
  methods: {
    login(formName){
      console.log(this.ruleForm);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          this.$http({
            url: '/admin/login',
            methods: 'post',
            params: this.ruleForm
          }).then(res => {
            console.log(res);
            this.$message({
              message: '登录成功',
              type: res == 1 ? 'success' : 'error'
            })
            if(res == 1){
              this.$router.push({
                path: '/home'
              })
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
      /* this.$router.push({
        path: '/home'
      }) */
    }
  }
}
</script>

<style scoped>
.login{
  width: 100%;
  height: 100%;
  background-image: url("../../assets/back.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  background-position: center;
  position: relative;
}
.box{
  width: 500px;
  height: 300px;
  background-color: white;
  border-radius: 6px;
  opacity: .9;
  position: absolute;
  top: 50%;
  left: 50%;
  margin-top: -150px;
  margin-left: -250px;
}
.box p{
  text-align: center;
  font-size: 22px;
  margin-bottom: 40px;
}
.el-input{
  width: 300px;
}
.el-button{
  width: 200px;
  margin-left: 150px;
}
</style>
